package com.mrl.queue;

import java.util.Scanner;

/**
 * 数组队列
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {
        // 创建队列
        ArrayQueue arrayQueue = new ArrayQueue(3);
        // 创建扫描器
        Scanner scanner = new Scanner(System.in);
        // 操作
        char o;
        //启动程序
        while (true) {
            System.out.println("================= 队列 ==================");
            System.out.println("[a]dd: 添加数据到队列");
            System.out.println("[g]et: 从队列取出一个数据");
            System.out.println("[h]ead: 查看队列头");
            System.out.println("[s]how: 查看队列列表");
            System.out.println("[e]xit: 退出");

            o = scanner.next().charAt(0);

            switch (o) {
                case 'a':
                    System.out.print("请输入要添加的数据:");
                    try {
                        arrayQueue.add(scanner.nextInt());
                    }catch (RuntimeException e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        System.out.println(arrayQueue.get());
                    }catch (RuntimeException e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        System.out.println(arrayQueue.head());
                    }catch (RuntimeException e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 's':
                    arrayQueue.show();
                    break;
                case 'e':
                    return;
                default:
                    System.out.println("输入错误");
            }
        }
    }
}

class ArrayQueue {
    private int maxSize;
    private int rear;
    private int front;
    private int[] arr;

    ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.rear = -1;
        this.front = -1;
        this.arr = new int[maxSize];
    }

    /**
     * 判断是否满
     * @return
     */
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    /**
     * 判断是否为空
     * @return
     */
    public boolean isEmpty() {
        return rear == front;
    }

    /**
     * 添加数据到队列
     */
    public void add(int data) {
        if (isFull()) {
            throw new RuntimeException("队列已满");
        }
        rear ++;
        arr[rear] = data;
    }

    /**
     * 从队列中取出数据
     */
    public int get() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arr[++ front];
    }

    /**
     * 读取队列头，但不取出
     */
    public int head() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front + 1];
    }

    /**
     * 显示队列
     */
    public void show() {
        for (int i = 0;i < arr.length;i++) {
            System.out.printf("Queue[%d] = %d \n", i, arr[i]);
        }
    }
}
